'use strict'
import { observable, action } from 'mobx'
import Cookies from 'js-cookie'
import asyncFetch from '../config/fetch'
import { getUserInfo, login } from '../config/mgtv'
import config from '../config/config'
const sigURL = config.resHeader + '/v1/doll/touch/signin'
const recordURL = config.resHeader + '/v1/doll/touch/successlist'
const balanceURL = config.resHeader + '/v1/doll/touch/balance'
const inviteCodeURL = config.resHeader + '/v1/doll/touch/getMyInviteCode'
const checkInviteCodeURL = config.resHeader + '/v1/doll/touch/checkInviteCode'
const signListURL = config.resHeader + '/v1/doll/touch/sign/list'
const signURL = config.resHeader + '/v1/doll/touch/sign/reward'
const messageURL = config.resHeader + '/v1/doll/notice/get_list_v1'
class User {
  @observable userInfo = {
    sig: false,
    uuid: '',
    ticket: '',
    avatar: '',
    nickname: '',
    newGiftNumber: 0
  }
  @observable balance = 0
  @observable recordList = null
  @observable messages = []
  @observable sigShow = 0
  @observable newGiftShow = 0
  @observable sigList = {}
  @observable sigShow = 0
  @observable newGiftShow = 0
  @observable shareInfo = {
    code: 0,
    shareUrl: '',
    tip: '',
    sharePic: '',
    shareTitle: '',
    shareText: ''
  }
  @observable signList = []
  @observable accSignDay = 0
  @observable isSign = 0
  signListIsFetch = 0
  isLogin = false
  type = false
  isBalance = false
  signIsFetch = 0
  messagesIsFetch = 0
  // 签到弹窗出现
  @action changeSigDisPlay = () => {
    if (this.sigShow === 1) { this.userInfo.sig = false }
    this.sigShow = this.sigShow === 0 ? 1 : 0
  }
  // 新手奖励弹窗出现
  @action changeNewGiftState = () => { this.newGiftShow = this.newGiftShow === 0 ? 1 : 0 }
  // 获取用户信息
  @action getUserInfo = async (type, flag) => {
    if (this.Cookies) { return }
    if (this.userInfo.uuid !== '') { return }
    try {
      let userInfo
      let showLoad = false
      if (type) {
        userInfo = await login()
        window.Qapp.showLoad('登录中')
        showLoad = true
      }
      else { userInfo = await getUserInfo() }
      if (userInfo) {
        const { uuid, ticket, avatar, nickname } = userInfo
        try {
          const res = await asyncFetch(sigURL, [{ uuid, ticket }], 'POST')
          if (res.err === 0) {
            Cookies.set('token', res.token)
            Cookies.set('uuid', uuid)
            if (flag) { return res }
            this.userInfo = {
              sig: this.userInfo.sig,
              uuid,
              ticket,
              avatar,
              nickname,
              newGiftNumber: res.new_comer_gift
            }
            if (res.new_comer_gift > 0) { this.changeNewGiftState() }
            this.isLogin = true
            this.getSignList()
            this.getInviteCode()
          }
          else { window.Qapp.showToast(res.err_msg) }
        }
        catch (error) {
          window.Qapp.showToast('网络错误')
        }
        if (showLoad) { window.Qapp.hideLoad() }
      }
    }
    catch (error) {
      console.log(error)
      window.Qapp.showToast('获取用户信息失败')
    }
  }
  // 获取抓中记录
  @action getRecordList = async (cb) => {
    try {
      if (this.type === 'successlist') { return }
      const res = await asyncFetch(recordURL, [null])
      if (res.err === 0) {
        this.recordList = res.record_list
        this.type = true
      }
      else { window.Qapp.showToast(res.err_msg) }
    }
    catch (error) {
      console.log(error)
      window.Qapp.showToast('获取列表失败')
    }
    if (cb) { cb() }
  }
  // 获取消息列表
  @action getMessages = async (cb) => {
    try {
      if (this.messagesIsFetch) { return }
      this.messagesIsFetch = true
      const res = await asyncFetch(messageURL, [null])
      if (res.err === 0) {
        this.messages = res.data
      }
      else { window.Qapp.showToast(res.err_msg) }
    }
    catch (error) {
      console.log(error)
      window.Qapp.showToast('获取列表失败')
    }
    this.messagesIsFetch = false
    if (cb) { cb() }
  }
  // 获取余额
  @action getBalance = async () => {
    try {
      const res = await asyncFetch(balanceURL, [null])
      if (res.err === 0) {
        this.isBalance = true
        this.balance = res.coin
      }
      else { window.Qapp.showToast(res.err_msg) }
    }
    catch (error) {
      console.log(error)
      window.Qapp.showToast('获取列表失败')
    }
  }
  // 获取邀请码页面信息
  @action getInviteCode = async (cb) => {
    try {
      const res = await asyncFetch(inviteCodeURL, [null])
      if (res.err === 0) {
        this.shareInfo = {
          code: res.code,
          shareUrl: res.share_redirect_url,
          tip: res.tip,
          sharePic: res.share_pic_url,
          shareTitle: res.share_title,
          shareText: res.share_text
        }
        const shareInfo = this.shareInfo
        const title = shareInfo.shareText
        const shareUrl = shareInfo.shareUrl
        const desc = shareInfo.shareTitle.replace('$', shareInfo.code)
        const shareIcon = shareInfo.sharePic
        try { MgtvApi.showShareMenus({ title, desc, shareUrl, shareIcon }) }
        catch (error) { console.log(error) }
      }
      else { window.Qapp.showToast(res.err_msg) }
    }
    catch (error) {
      console.log(error)
      window.Qapp.showToast('获取列表失败')
    }
  }
  // 发送邀请码
  @action checkInviteCode = async (code) => {
    try {
      const res = await asyncFetch(checkInviteCodeURL, [null, { code }], 'POST')
      if (res.err === 0) {
        window.Qapp.showToast(res.tip)
      }
      else { window.Qapp.showToast(res.err_msg) }
    }
    catch (error) {
      console.log(error)
      window.Qapp.showToast('提交失败')
    }
  }
  // 获取主页信息
  @action getHomeData = async () => {
    if (this.userInfo.nickname === '') {
      await this.getUserInfo()
    }
    await this.getRecordList()
    await this.getBalance()
  }
  // 获取签到列表
  @action getSignList = async function (cb) {
    if (this.signListIsFetch) { return }
    this.signListIsFetch = true
    try {
      const res = await asyncFetch(signListURL, [{}])
      if (res.err === 0) {
        window.console.log(res)
        this.signList = res.feeds
        this.accSignDay = res.acc_sign_day
        this.isSign = res.is_sign
        this.userInfo.sig = res.is_sign !== 1
        // this.userInfo.sig = true
      }
      else { window.Qapp.showToast(res.err_msg) }
    }
    catch (error) {
      console.log(error)
      window.Qapp.showToast('网络错误')
    }
    this.signListIsFetch = false
    if (cb) { cb() }
  }
  // 签到
  @action signToday = async (cb) => {
    if (this.signIsFetch) { return }
    this.signIsFetch = true
    try {
      const res = await asyncFetch(signURL, [{}], 'POST')
      if (res.err === 0) {
        window.console.log('签到成功')
      }
      else { window.Qapp.showToast(res.err_msg) }
    }
    catch (error) {
      console.log(error)
      window.Qapp.showToast('网络错误')
    }
    this.signIsFetch = false
    if (cb) { cb() }
  }
  // 修改余额
  @action changeBalance = (coin) => { this.balance = coin }
}

export default new User()
